//
//  DataEngine.m
//  AutoHome
//
//  Created by wqh on 15-5-11.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "DataEngine.h"
#import "AFNetworking.h"
#import "BrandCarModel.h"
#import "CarOperationModel.h"

@implementation DataEngine
{
    AFHTTPRequestOperationManager *_httpManager;
}
+ (instancetype)sharedInstance {
    static DataEngine *dataEngine = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataEngine = [[DataEngine alloc] init];
    });
    return dataEngine;
}
- (instancetype)init {
    if (self = [super init]) {
        _httpManager = [[AFHTTPRequestOperationManager alloc]init];
        //返回二进制
        _httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    return self;
}

//搜索
- (void)requestSearchDataWithStr:(NSString *)str Page:(NSInteger )pageIndex succsee:(SuccessBlockType)success failed:(FailedBlockType)failed{
    
    
    
    NSString *url = [NSString stringWithFormat:kSearchResultUrl,str,pageIndex];
    NSString * encodingString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   
    [_httpManager GET:encodingString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

//推荐菜单 普通详情页面数据请求
- (void)requestWebViewDataWithId:(NSNumber *)Id success:(SuccessBlockType)success failed:(FailedBlockType)failed{
    
    NSString *url = [NSString stringWithFormat:kRecommendNormalDetailUrl,Id.stringValue];
  
    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failed(error);
    }];
}
//推荐菜单 特殊页面详情数据请求
- (void)requestSpecialWebViewDataWith:(NSString *)url id:(NSNumber*)Id success:(SuccessBlockType)success failed:(FailedBlockType)failed {
    [_httpManager GET:[NSString stringWithFormat:url,Id.stringValue] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

//推荐菜单 快报详情
- (void)requestNewsflahDetailDataWithId:(NSNumber *)Id success:(SuccessBlockType)success failed:(FailedBlockType)failed {
    NSString *url = [NSString stringWithFormat:kRecommendDetailNewsflashUlr,Id.stringValue];
    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

//推荐菜单 最新页面中多张图片的详情接口
- (void)requestPicsDataWithId:(NSString *)id1 :(NSString *)id2 success:(SuccessBlockType)success failed:(FailedBlockType)failed{
    NSString *url = [NSString stringWithFormat:kRecommendDetailPicsUrl,id1,id2];
  
    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

//对点击车牌后的页面数据进行请求；
- (void)requestBrandCarWithBrandId:(NSNumber *)brandId typeId:(NSInteger)typeId success:(SuccessBlockType)success failed:(FailedBlockType)failed {
    NSString *ID = [brandId stringValue];
    NSString *url = [NSString stringWithFormat:kBrandCarUrl,ID,typeId];
    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

//通过筛选条找查找符合的车辆
/*
 //根据筛选条件查找车辆的接口
 
 bid 品牌id
 mip 最低价格
 map 最高价格
 l 级别
 c 国别
 b 变速箱
 st 结构
 mid 最低排量
 mad 最高排量
 f 燃料
 conf 配置
 
#define kQueryCarListUrl @"http://app.api.autohome.com.cn/autov4.7/cars/series-pm1-mip0-map0-l0-c0-b0-st0-mid0-mad0-conf0-o0-p1-s20-bid0-f0.json"
*/
- (void)requestCarListWithDictionary:(NSDictionary *)dict pageIndex:(NSInteger)page Success:(SuccessBlockType)success failed:(FailedBlockType)failed{
    
   
    NSString *url = [NSString stringWithFormat:kQueryCarListUrl,dict[@"最低价格"],dict[@"最高价格"],dict[@"级别"],dict[@"国别"],dict[@"变速箱"],dict[@"结构"],dict[@"最低排量"],dict[@"最高排量"],dict[@"配置"],page,dict[@"品牌"],dict[@"燃料"]];
    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

- (void)requestCarTypeDetailPriceWithSystemId:(NSNumber *)systemId typeId:(NSNumber *)typeId cityId:(NSNumber *)cityId Success:(SuccessBlockType)success failed:(FailedBlockType)failed {
    NSString *url = [NSString stringWithFormat:kTypeCarDetailPricelUrl,typeId.stringValue,systemId.stringValue,@"110100"];

    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

- (void)requestCarTypeDetailInfoWithTypeId:(NSNumber *)typeId cityId:(NSNumber *)cityId Success:(SuccessBlockType)success failed:(FailedBlockType)failed {
    NSString *url = [NSString stringWithFormat:kTypeCarPriceInfoUrl,typeId,@"110100"];

    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}

- (void)requestCarBrandPicDetailWithId:(NSString *)brandId success:(SuccessBlockType)success failed:(FailedBlockType)failed {
    NSString *url = [NSString stringWithFormat:kBrandPicDetailUrl,brandId];
    [_httpManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failed) {
            failed(error);
        }
    }];
}


#pragma mark - parseData 


- (NSMutableArray *)parseBrandData {
    NSString *allBrandFilePath = [[NSBundle mainBundle] pathForResource:@"allBrand" ofType:@"json"];
    
    NSMutableArray *allBrandArr = [NSMutableArray array];
    NSMutableArray *letterArr = [NSMutableArray array];
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:allBrandFilePath] options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *brandlist = dict[@"result"][@"brandlist"];
    
    for (NSDictionary *listDict in brandlist) {
        NSString *key = listDict[@"letter"];
        [letterArr addObject:key];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *carDict in listDict[@"list"]) {
            BrandCarModel *model = [[BrandCarModel alloc] init];
            [model setValuesForKeysWithDictionary:carDict];
            [array addObject:model];
        }
        [allBrandArr addObject:array];
    }
    NSMutableArray *totalArr = [NSMutableArray arrayWithObjects:letterArr, allBrandArr, nil];
    return totalArr;
}


- (NSMutableDictionary *)parseCarOperationData {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"carOperation" ofType:@"json"];
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:filePath] options:NSJSONReadingMutableContainers error:nil];
    
    NSMutableDictionary *operationDict = [[NSMutableDictionary alloc] init];
    NSArray *carOperationList = dict[@"result"][@"metalist"];
    for (NSDictionary *listDict in carOperationList) {
        NSMutableArray *array = [NSMutableArray array];
        NSString *key = listDict[@"key"];
        for (NSDictionary *carDict in listDict[@"list"]) {
            
            CarOperationModel *model = [[CarOperationModel alloc] init];
            [model setValuesForKeysWithDictionary:carDict];
            [array addObject:model];
        }
        [operationDict setObject:array forKey:key];
//        [operationDict setValue:array forKey:key];
    }
    return operationDict;
}






@end
